package com.qyer.commons.blender;

import static com.qyer.commons.blender.Blender.nextInt;

import java.util.Random;

/**
 * User: Z J Wu Date: 2015/09/24 Time: 01:10 Package: com.qyer.dsp.model.blend
 */
public class BlenderLayoutPattern<BLEND_KEY> {

  public static final int DEFAULT_COUNT_PER_KEY = 3;

  private BLEND_KEY key;

  /*
   * FIXME: 2016/03/03 Layout还可增加最小值限制
   * 目前只支持指定最大随机值, 也就是只能输出1-n的随机值.
   * 后续还可以加入m-n随机值
   */
  private int repeatNum;

  private Random random;

  public BlenderLayoutPattern(BLEND_KEY key, int repeatNum) {
    this.key = key;
    this.repeatNum = repeatNum;
  }

  public BlenderLayoutPattern(BLEND_KEY key) {
    this.key = key;
    this.random = new Random();
  }

  public BLEND_KEY getKey() {
    return key;
  }

  public void setKey(BLEND_KEY key) {
    this.key = key;
  }

  public int getInitRepeatNum() {
    return repeatNum;
  }

  public int getRepeatNum(int forceRepeatCnt) {
    if (repeatNum > 0) {
      return repeatNum;
    }
    int cnt = forceRepeatCnt <= 0 ? DEFAULT_COUNT_PER_KEY : forceRepeatCnt;
    return nextInt(random, cnt);
  }

  public void setRepeatNum(int repeatNum) {
    this.repeatNum = repeatNum;
  }

  @Override public String toString() {
    if (repeatNum < 1) {
      return "(" + key.toString() + " repeat random)";
    }
    return "(" + key.toString() + " repeat " + repeatNum + ")";
  }
}
